@extends('template')
@section('css')
<style>
    .btn-danger{
        position: fixed;
        bottom: 0;
    }
</style>
@stop
@section('content')
<header class="m-navbar">
    <a href="{{route('ucenter')}}" class="navbar-item">
        <i class="back-ico"></i>
    </a>
    <div class="navbar-center">
        <span class="navbar-title">注册</span>
    </div>
</header>

<div class="g-scrollview">
    <form action="" method="post" id="form">
        <div class="m-cell">
            <div class="cell-item">
                <div class="cell-left">昵称：</div>
                <div class="cell-right"><input name="username" value="{{$data->username}}" id="username" required type="text" maxlength="20" class="cell-input" placeholder="昵称可以是真实姓名" autocomplete="off" /></div>
            </div>
            <div class="cell-item">
                <div class="cell-left">旧密码：</div>
                <div class="cell-right"><input required id="oldpwd" type="password" class="cell-input passwordone" placeholder="请输入旧密码" autocomplete="off" /></div>
            </div>
            <div class="cell-item">
                <div class="cell-left">新密码：</div>
                <div class="cell-right"><input name="password" id="password" required type="password" class="cell-input password" placeholder="请输入新密码" autocomplete="off" /></div>
            </div>
            <div class="cell-item">
                <div class="cell-left">QQ：</div>
                <div class="cell-right"><input name="qq" value="{{$data->qq}}" disabled type="number" class="cell-input" placeholder="请输入QQ号码（QQ号码用于获取头像）" autocomplete="off" /></div>
            </div>
            <button type="submit" class="btn-block btn-danger">修改</button>
        </div>
    </form>
</div>
@stop
@section('js')
<script>
    !function (win,$) {
        var dialog = win.YDUI.dialog;
        $('#form').submit(function () {
            var oldpwd = $('#oldpwd').val();
                pwd = $('.password').val();
            if (oldpwd == pwd){
                dialog.toast('新旧密码一致！', 'error', 1500);
                return false;
            }
            var reg = /^[A-Za-z0-9]{6,16}$/;
            if (!reg.test(pwd)){
                dialog.toast('密码必须由 6-16位字母、数字组成！', 'error', 1500);
                return false;
            }
            /* ajax发送前显示加载中提示 */
            YDUI.dialog.loading.open('修改账号密码中...');
            $.ajax({
                type:'post',
                url:"{{route('updateuser')}}",
                data:{
                    username:$('#username').val(),
                    password:$('#password').val(),
                    oldpwd:$('#oldpwd').val()
                },
                success:function (result) {
                    if (result){
                        YDUI.dialog.toast('修改成功！', 'success', 1200, function () {
                            /* 关闭后调用 */
                            win.location.href="{{route('ucenter')}}";
                        });
                    }else {
                        dialog.toast('修改失败(原密码错误)!', 'error', 2000);
                    }
                },
                complete: function() {
                    /* 结束后隐藏加载中提示 */
                    YDUI.dialog.loading.close();
                }
            });
            return false;
        });
    }(window,jQuery);
</script>
@stop
